using System.IO;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.UI;
using NetMf.CommonExtensions;

namespace Melody_Glider.GUIContent
{
    class LoadMenuWindow: WindowFrame
    {
        private readonly GUI _gui;
        private readonly SystemState _conditions;
        private int _selectedRowIndex;

        public LoadMenuWindow(GUI gui, SystemState state)
        {
            _gui = gui;
            _conditions = state;
        }
        
        protected override void SophisticatedInit()
        {
            InitFileGrid();
        }

        private void InitFileGrid()
        {
            var fileGrid = (DataGrid)ThisWindow.GetChildByName("FileGrid");
            fileGrid.AddColumn(new DataGridColumn("File name", 300));
            fileGrid.TapCellEvent += FileGridTapCellEvent;
            fileGrid.Sort(0); //todo: waiting for another Glide with case-ignore sorting
        }

        private void FileGridTapCellEvent(object sender, TapCellEventArgs args)
        {
            var fileGrid = (DataGrid)ThisWindow.GetChildByName("FileGrid");

            if (fileGrid.GetRowData(args.RowIndex) == null)
            {
                fileGrid.SelectedIndex = -1;
                DisableFunctions();
                return;
            }

            _selectedRowIndex = args.RowIndex;

            var fileName = fileGrid.GetRowData(_selectedRowIndex)[0];

            _conditions.ActiveFileName = fileName.ToString();
            _conditions.ActiveFileName = _conditions.ActiveFileName.Substring(0, _conditions.ActiveFileName.Length - 4);

            EnableFunctions();

            fileGrid.Invalidate();
        }

        protected override void InitButtons()
        {
            var playBtn = (Button)ThisWindow.GetChildByName("PlayButton");
            playBtn.TapEvent += PlayBtnTapEvent;

            var editBtn = (Button)ThisWindow.GetChildByName("EditButton");
            editBtn.TapEvent += EditBtnTapEvent;

            var delBtn = (Button)ThisWindow.GetChildByName("DelButton");
            delBtn.TapEvent += DelBtnTapEvent;

            var cancelBtn = (Button)ThisWindow.GetChildByName("CancelButton");
            cancelBtn.TapEvent += CancelBtnTapEvent;
        }

        protected override void PrepareToMain()
        {
            DisableFunctions();
            RefreshGrid();
        }

        private void EnableFunctions()
        {
            _gui.ToggleElement("PlayButton", ThisWindow, true);
            _gui.ToggleElement("EditButton", ThisWindow, true);
            _gui.ToggleElement("DelButton", ThisWindow, true);
        }

        private void DisableFunctions()
        {
            _gui.ToggleElement("PlayButton", ThisWindow, false);
            _gui.ToggleElement("EditButton", ThisWindow, false);
            _gui.ToggleElement("DelButton", ThisWindow, false);
        }

        private void RefreshGrid()
        {
            var fileGrid = (DataGrid)ThisWindow.GetChildByName("FileGrid");
            fileGrid.Clear(); //for the first time it's empty but I don't find it troubling too much

            foreach (var filename in Directory.GetFiles(_conditions.Drive.Volume.RootDirectory))
            {
                if (filename.Substring(filename.Length-4, 4).ToLower() != ".tsm")
                    continue;

                var gridItem = new DataGridItem(new[] { filename.Replace(_conditions.Drive.Volume.RootDirectory + "\\", "") });
                fileGrid.AddItem(gridItem);
            }

            fileGrid.Invalidate();
        }
        
        private void PlayBtnTapEvent(object sender)
        {
            /*var fileGrid = (DataGrid)ThisWindow.GetChildByName("FileGrid");
            var fileName = fileGrid.GetRowData(_selectedRowIndex)[0];
            var path = _conditions.Drive.Volume.RootDirectory + @"\" + fileName;
            string melodyStringBuffer;
            using (var inStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var sr = new StreamReader(inStream);
                melodyStringBuffer = sr.ReadToEnd();

                sr.Close();
                inStream.Close();
            }

            _conditions.MelodyBuffer = new Melody();
            _conditions.MelodyBuffer.Parse(melodyStringBuffer);
            _gui.DisableButton("PlayButton", ThisWindow);
            _conditions.MelodyBuffer.PlayMelody(); //todo: it shouldn't play anything when parse fails
            _gui.EnableButton("PlayButton", ThisWindow);*/
        }

        private void EditBtnTapEvent(object sender)
        {
            /*var fileGrid = (DataGrid)ThisWindow.GetChildByName("FileGrid");
            var fileName = fileGrid.GetRowData(_selectedRowIndex)[0];
            var path = _conditions.Drive.Volume.RootDirectory + @"\" + fileName;
            string melodyStringBuffer;
            using (var inStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var sr = new StreamReader(inStream);
                melodyStringBuffer = sr.ReadToEnd();

                sr.Close();
                inStream.Close();
            }
            _conditions.MelodyBuffer = new Melody();
            _conditions.MelodyBuffer.Parse(melodyStringBuffer); //todo: if parse fails it shouldn't continue
            _conditions.SetForLoadedFile();
            _gui.Cursor.Position = -1;
            _gui.EditorWindow.SetToMain();*/
        }

        private void DelBtnTapEvent(object sender)
        {
            _gui.DeleteQuestionBox.SetToMain();
        }

        private void CancelBtnTapEvent(object sender)
        {
            _gui.MainMenuWindow.SetToMain();

            var fileGrid = (DataGrid)ThisWindow.GetChildByName("FileGrid");
            fileGrid.ScrollTo(0);
        }
    }
}
